package ck.android.englishschool.thread;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;
import ck.android.englishschool.AppStatus;
import ck.android.englishschool.util.BrUtilManager;
import ck.android.englishschool.util.Const;
import ck.android.englishschool.util.JSONParser;
import ck.android.englishschool.util.ParameterUtil;
import ck.android.englishschool.vo.WordListVO;

public class WordListThread extends BaseThread{
	
	public WordListThread(Context context, ThreadCallBack callBack
			, HashMap<String, String> searchMap){
		this.searchMap = searchMap;
		this.context = context;
		this.callBack = callBack;
		this.appStatus = (AppStatus)context.getApplicationContext();
		
		System.out.println(searchMap.get("targetPage").toString()+"<-- 맵에 잇는거");
	}
	
	@Override
	public void run() {
		if(!appStatus.utilm.isNetworkConnected(context)){
			callBack.threadCompleted(false, false, "통신 에러");
			return;
		}
		
		System.out.println("들어왔냐?");
		
		String sDate = BrUtilManager.getCurrentDateTime(0);
		String eDate = BrUtilManager.getCurrentDateTime(0);
		
		searchMap.put(Const.SEARCH_SDATE, sDate);
		searchMap.put(Const.SEARCH_EDATE, eDate);
		
		JSONParser jParser = new JSONParser();
		
		url = Const.MAIN_URL;
		post = ParameterUtil.getWordListParams(searchMap);
		
		JSONObject json = jParser.getJSONFromUrl(url, post);
		Log.i("url","url = " + url + post);
		Log.i("json","json = " + json);
		
		if(json == null){
			callBack.threadError("데이터가 없습니다.");
			return;
		}else{
			try {
				int result = json.getInt(Const.RESULT);
				System.out.println("result -> "+result);
				
				ArrayList<WordListVO> arrWordListVO = null;
				if(pageState){
					arrWordListVO = appStatus.getArrWordList();
				}else{
					arrWordListVO = new ArrayList<WordListVO>();
				}
				System.out.println("arrWordListVO -> "+arrWordListVO.toString());
				
				if(result == 0){
					System.out.println("데이터 못가져 왓을때");
					String message = json.getString(Const.MESSAGE);
					callBack.threadError(message);
				}else{
					System.out.println("데이터 가져 왓을때");
					//String message = json.getString(Const.MESSAGE);
					JSONArray jArray = json.getJSONArray(Const.DATA);
					Log.i("arrayWordList", "->"+jArray);
					WordListVO vo = null;
					for(int i=0; i<jArray.length(); i++){
						JSONObject obj = jArray.getJSONObject(i);
						vo = new WordListVO();
						vo.setMemSeq(appStatus.utilm.getStringJSONObject(obj, WordListVO.LISt_SEQ));
						vo.setTitle(appStatus.utilm.getStringJSONObject(obj, WordListVO.TITLE));
						vo.setRegDate(appStatus.utilm.getStringJSONObject(obj, WordListVO.REG_DATE));
						vo.setUpDate(appStatus.utilm.getStringJSONObject(obj, WordListVO.UP_DATE));
						vo.setIdentyfy(appStatus.utilm.getStringJSONObject(obj, WordListVO.IDENTYFY));
						vo.setUserLevel(appStatus.utilm.getStringJSONObject(obj, WordListVO.USER_LEVEL));
						arrWordListVO.add(vo);
					}
					appStatus.setArrWordList(arrWordListVO);
					callBack.threadCompleted(true, true, "성공");
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		
	}
}

















